package cn.Alibaba.user.controller;

import cn.Alibaba.basic.exception.BusinessException;
import cn.Alibaba.basic.util.JsonResult;
import cn.Alibaba.user.domain.Logininfo;
import cn.Alibaba.user.dto.BinderDto;
import cn.Alibaba.user.dto.LoginDto;
import cn.Alibaba.user.service.ILogininfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILogininfoService logininfoService;

    @PostMapping("/account")
    public JsonResult getLoginin(@RequestBody LoginDto dto){

        try {
            Map<String,Object> map = logininfoService.getLoginin(dto);
            return JsonResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网路繁忙！请稍后重试");
        }
    }

    @GetMapping("/wechat/{code}")
    public JsonResult wechatLoginin(@PathVariable("code") String code){

        try {

            return logininfoService.wechatLoginin(code);
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网路繁忙！请稍后重试");
        }
    }

    @PostMapping("/wechat/binder")
    public JsonResult wechatBinder(@RequestBody BinderDto binderDto){

        try {
            return logininfoService.wechatBinder(binderDto);
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网路繁忙！请稍后重试");
        }
    }



    @GetMapping("/logout/{token}")
    public JsonResult logout(@PathVariable("token") String token){

        try {
            logininfoService.logout(token);
            return new  JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网路繁忙！请稍后重试");
        }
    }
}
